<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport( 'joomla.utilities.date' );

class iJoomla_SurveysModelConfig extends JModel{

	function getComponentParams(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('params');
		$query->from('#__extensions');
		$query->where("name='com_surveys'");
		$db->setQuery($query);		
		$db->query();
		$result_string = $db->loadResult();
		$result = json_decode($result_string);
		return $result;
	}
	
	function saveParams(){
		$params = array();
		$css = JRequest::getVar("css", "", "post");
		
		$params["general_value"] = JRequest::getVar("general_value", "", "post");
		$params["general_date"] = JRequest::getVar("general_date", "", "post");		
		$params["survey_name"] = $css["survey_name"];
		$params["survey_description"] = $css["survey_description"];
		$params["page_name"] = $css["page_name"];
		$params["page_description"] = $css["page_description"];
		$params["question"] = $css["question"];
		$params["question_description"] = $css["question_description"];
		$params["answer"] = $css["answer"];
		$params["checkbox"] = $css["checkbox"];
		$params["radiobutton"] = $css["radiobutton"];
		$params["dropdownmenu"] = $css["dropdownmenu"];		
		$params["textarea"] = $css["textarea"];
		$params["button"] = $css["button"];
		$params["row_heading"] = $css["row_heading"];
		$params["column_heading"] = $css["column_heading"];
		$params["table_row1"] = $css["table_row1"];
		$params["table_row2"] = $css["table_row2"];
		$params["result_heading"] = $css["result_heading"];
		$params["total_background"] = $css["total_background"];
		$params["popup_title"] = $css["popup_title"];
		$params["popup_content"] = $css["popup_content"];
		$params["popup_button_row"] = $css["popup_button_row"];
	
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->update("#__extensions");
		$query->set("`params`='".json_encode($params)."'");
		$query->where("`element`='com_surveys'");
		$db->setQuery($query);		
		if(!$db->query()){
			return false;
		}
		return true;
	}
	
	function getEmailSettings(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('*');
		$query->from('#__ijoomla_surveys_email_settings');
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();
		return $result;
	}
	
	function getLanguage(){
		$language = "";
		$admin_filename = JPATH_ROOT.DS."administrator".DS."language".DS."en-GB".DS."en-GB.com_surveys.ini";
		$handle = fopen($admin_filename, 'r');
		$language_file = fread($handle, filesize($admin_filename));
		$language = $language_file;		
		return $language;
	}
	
	function saveLanguage(){
		$language = "";
		$admin_filename = JPATH_ROOT.DS."administrator".DS."language".DS."en-GB".DS."en-GB.com_surveys.ini";
		$handle = fopen($admin_filename, 'w');
		if(fwrite($handle, JRequest::getVar("lang_file"))){
			fclose($handle);
			return true;
		}
		return false;
	}
	
	function getCSS(){
		$css = "";
		$css_filename = JPATH_ROOT.DS."components".DS."com_surveys".DS."css".DS."survey.css";
		$handle = fopen($css_filename, 'r');
		$css_file = fread($handle, filesize($css_filename));
		$css = $css_file;		
		return $css;
	}
	
	function saveCSS(){
		$css = "";
		$css_filename = JPATH_ROOT.DS."components".DS."com_surveys".DS."css".DS."survey.css";
		$handle = fopen($css_filename, 'w');
		if(fwrite($handle, JRequest::getVar("css_file"))){
			fclose($handle);
			return true;
		}
		return false;
	}
	
	function saveEmail(){
		$email_notification = JRequest::getVar("email_activ");
		$to = JRequest::getVar("email_to");
		$from = JRequest::getVar("email_from");
		$subject = JRequest::getVar("email_subject");
		$content = JRequest::getVar("editor1", JREQUEST_ALLOWRAW);
				
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('count(*)');
		$query->from('#__ijoomla_surveys_email_settings');
		$db->setQuery($query);		
		$db->query();		
		$result = $db->loadResult();
		if($result != NULL || $result != "0"){
			$query->clear();
			$query->update("#__ijoomla_surveys_email_settings");
			$query->set("`email_notification`='".$email_notification."'");
			$query->set("`to`='".addslashes($to)."'");
			$query->set("`from`='".addslashes($from)."'");
			$query->set("`subject`='".addslashes($subject)."'");
			$query->set("`content`='".addslashes($content)."'");
			$db->setQuery($query);			
			if(!$db->query()){
				return false; 
			}
			return true;
		}
		else{
			$query->clear();
			$query->insert("#__ijoomla_surveys_email_settings");
			$query->set("`to`=".$to);
			$query->set("`from`=".$from);
			$query->set("`subject`=".$subject);
			$query->set("`content`=".$content);
			$db->setQuery($query);			
			if(!$db->query()){
				return false; 
			}
			return true;
		}
	}
	
	function save(){
		if(!$this->saveParams()){
			return false;
		}
		
		if(!$this->saveCSS()){
			return false;
		}
		
		if(!$this->saveLanguage()){
			return false;
		}
		
		if(!$this->saveEmail()){
			return false;
		}
		
		return true;
	}
}

?>